import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'calculator_controller.dart';

class CalculatorPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<CalculatorController>(
        init: CalculatorController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(title: Text('计算器')), body: _keyboard(controller));
        });
  }

  _keyboard(CalculatorController controller) {
    return Container(
        color: Colors.white,
        padding: const EdgeInsets.only(bottom: 15),
        // height: windowHeight * 0.5,
        // width: windowWidth * 0.5,
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Container(
              alignment: Alignment.centerRight,
              width: windowWidth * 0.32,
              height: 40,
              child: InkWell(
                  onLongPress: () {
                    controller.clearPrice();
                  },
                  child: Text(
                    controller.price.value.toString(),
                    style: TextStyle(color: Colors.redAccent, fontSize: 20),
                  ))),
          SizedBox(
              width: windowWidth * 0.35,
              child: Row(
                children: [
                  _textButton('1', controller, onPressed: () {
                    controller.changePrice('1');
                  }),
                  _textButton('2', controller, onPressed: () {
                    controller.changePrice('2');
                  }),
                  _textButton('3', controller, onPressed: () {
                    controller.changePrice('3');
                  }),
                  _textButton('←', controller, onPressed: () {
                    controller.deletePrice();
                  }, onLongPressed: () {
                    controller.clearPrice();
                  })
                ],
              )),
          SizedBox(
              width: windowWidth * 0.35,
              child: Row(
                children: [
                  _textButton('4', controller, onPressed: () {
                    controller.changePrice('4');
                  }),
                  _textButton('5', controller, onPressed: () {
                    controller.changePrice('5');
                  }),
                  _textButton('6', controller, onPressed: () {
                    controller.changePrice('6');
                  }),
                  _textButton('-', controller, onPressed: () {
                    controller.minus();
                  })
                ],
              )),
          SizedBox(
              width: windowWidth * 0.35,
              child: Row(
                children: [
                  _textButton('7', controller, onPressed: () {
                    controller.changePrice('7');
                  }),
                  _textButton('8', controller, onPressed: () {
                    controller.changePrice('8');
                  }),
                  _textButton('9', controller, onPressed: () {
                    controller.changePrice('9');
                  }),
                  _textButton('+', controller, onPressed: () {
                    controller.plus();
                  })
                ],
              )),
          SizedBox(
              width: windowWidth * 0.35,
              child: Row(
                children: [
                  _textButton('0', controller, onPressed: () {
                    controller.changePrice('0');
                  }),
                  _textButton('.', controller, onPressed: () {
                    controller.changePrice('.');
                  }),
                  _textButton('x', controller, onPressed: () {
                    controller.multiply();
                  }),
                  _textButton('=', controller, onPressed: () {
                    controller.calculate();
                  }),
                ],
              )),
        ]));
  }

  _textButton(String text, CalculatorController controller,
      {Function()? onPressed,
      Function()? onLongPressed,
      Color textColor = Colors.black,
      double fontSize = 20,
      bool isSound = true}) {
    return Container(
        margin: const EdgeInsets.symmetric(horizontal: 7, vertical: 7),
        child: SizedBox(
          width: 90,
          height: 40,
          child: TextButton(
              style: TextButton.styleFrom(
                  //背景颜色
                  backgroundColor: Colors.white,
                  shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  side: const BorderSide(
                      color: Colors.deepPurpleAccent, width: 1)),
              onPressed: () {
                if (isSound) {
                  // Vibrate.feedback(FeedbackType.light);
                }
                onPressed?.call();
              },
              onLongPress: () {
                onLongPressed?.call();
              },
              child: Text(text,
                  style: TextStyle(color: textColor, fontSize: fontSize))),
        ));
  }
}
